﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
using CefSharp;
using Microsoft.Win32;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Timer = System.Timers.Timer;

namespace mytools
{
    /**
    * 这个类就是提供给前端调用的执行类
    */
    public class CEFJSBridge
    {
        private Form1 form;

        public ApplicationInfo Application { get; set; }
        public string UnlockCode { get; set; }

        public CEFJSBridge(Form1 form)
        {
            this.form = form;//k
        }

        public void MinimizeWindow()
        {
            // 在创建控件的线程上执行关闭操作
            if (Form.ActiveForm != null)
            {
                Form form = Form.ActiveForm;
                form.Invoke((MethodInvoker)delegate
                {
                    // 关闭当前窗口
                    form.WindowState = FormWindowState.Minimized;
                });
            }
        }

        [STAThread]
        public void jsClickItem(string Content)
        {
            // 在窗体的创建线程上执行操作
            if (Form1.window2.InvokeRequired)
            {
                Form1.window2.Invoke(new MethodInvoker(delegate
                {
                    Form1.window2.WindowState = FormWindowState.Minimized;
                    Thread.Sleep(200);
                    // 将点击的内容粘贴到当前鼠标光标处
                    Clipboard.SetText(Content);
                    // 发送粘贴命令
                    //SendKeys.Send("^(v)");
                    SendKeys.Send("+{INSERT}");
                }));
            }
            else
            {
                Form1.window2.WindowState = FormWindowState.Minimized;
                Thread.Sleep(200);
                // 将点击的内容粘贴到当前鼠标光标处
                Clipboard.SetText(Content);
                // 发送粘贴命令
                //SendKeys.Send("^(v)");
                SendKeys.Send("+{INSERT}");
            }

        }

        public void MoveWindow(int x, int y)
        {
            // 在创建控件的线程上执行关闭操作
            if (Form.ActiveForm != null)
            {
                Form form = Form.ActiveForm;
                form.Invoke((MethodInvoker)delegate
                {
                    form.Location = new Point(x, y);
                });
            }
        }

        public void CloseWindow()
        {
            // 在创建控件的线程上执行关闭操作
            if (Form.ActiveForm != null)
            {
                Form form = Form.ActiveForm;
                form.Invoke((MethodInvoker)delegate
                {
                    // 关闭当前窗口
                    form.Close();
                });
            }
        }

        private static void MExited(object sender, EventArgs e)
        {
            // 终止应用程序
            Environment.Exit(1);
        }


        public class ApplicationInfo
        {
            public string Version { get; set; }//必须有get set方法
            public string Title { get; set; }
            public string Icon { get; set; }
            public string RId { get; set; }
            public string Qrcode { get; set; }
            public string DefaultCode { get; set; }
        }

        public void GoForward()
        {
            if (this.form.browser.CanGoForward)
            {
                this.form.browser.Forward();
            }
        }

        public void GoBack()
        {
            if (this.form.browser.CanGoBack)
            {
                this.form.browser.Back();
            }
        }

        public void Reload()
        {
            this.form.browser.Reload();
        }
    }

}
